﻿using System.Linq;
using System.Web.Mvc;
using JStore.Tasks;
using JStore.Web.Controllers.News.ViewModels;
using SpeadFrm.Mappers;
using SpeadFrm.MvcPager;

namespace JStore.Web.Controllers.News
{
    public class NewsController : BaseController
    {
        private readonly NewsTask _newsTask = new NewsTask();
        private readonly SysMsgTask _sysMsgTask = new SysMsgTask();

        public ActionResult List(int pageIndex = 1, int pageSize = 10)
        {
            ViewData["MsgCount"] = _sysMsgTask.GetCount(LogOnUserName);

            var pagedList = _newsTask.GetPagedList(pageIndex, pageSize);
            var items = pagedList.Select(EntityMapper.Map<Core.Entities.News, NewsModel>).ToList();
            var model = new PagedList<NewsModel>(items, pageIndex, pageSize, pagedList.TotalItemCount);
            return View(model);
        }

        public ActionResult Info(int id)
        {
            var info = _newsTask.GetById(id);
            if (info == null) return Content("参数异常");
            var model = EntityMapper.Map<Core.Entities.News, NewsModel>(info);
            return View(model);
        }
    }
}